<?php

namespace backend\modules\demo\controllers;

//use backend\modules\content\models\search\ArticleSearch;
use backend\modules\demo\models\Customer;
use backend\modules\demo\models\CustomerAlias1;
use backend\modules\demo\models\CustomerAlias2;
use backend\modules\demo\models\EsLog;
use backend\modules\system\models\SystemLog;
use backend\modules\demo\models\search\EsSearch;
use yii\elasticsearch\Query;
use yii\filters\VerbFilter;
use yii\web\Controller;
use Yii;
//use yii\elasticsearch\ActiveDataProvider;
//use yii\elasticsearch\Query;

/**
 *
 *
 *
 * Class EsController
 * @package backend\modules\demo\controllers
 */
class EsController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::class,
                'actions' => [
                    'delete' => ['post'],
                    'clear' => ['post'],
                ],
            ],
        ];
    }


    /**
     * dataProvider列表
     * Renders the index view for the module
     * @return string
     */
    public function actionIndex()
    {

        $searchModel = new EsSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        if (strcasecmp(Yii::$app->request->method, 'delete') == 0) {
            EsLog::deleteIndex();
            return $this->refresh();
        }

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);

    }
    /**
     * 编辑
     * @throws \yii\elasticsearch\Exception
     */

    //Aggregation
    public function actionAgg(){

        $query = new Query();
        $query->from('es-logs', 'es-log');
        $query->addAggregate('aggs', [
            "stats"=>["field"=>"level"]]
        );
        $query->source(false);
        $bb = $query->search();
        print_r($bb);die;
    }


    /**
     * 添加结构
     */
    public function actionUpdateMapping(){
        $alias = Customer::bulkData();
        var_dump($alias);die;



        $alias = CustomerAlias2::createIndex();
        var_dump($alias);die;
        $test = Customer::updateMapping();die;
    }

    /**
     * 洗数据
     * 别名
     */

    public function actionAlias(){
        $alias = Customer::aliasActions();
    }

}
